import { updateProfile } from "../api/otherServer";
import { Notify } from "vant";
export default {
  namespaced: true,
  state: {
    profile: localStorage.getItem("profile")
      ? JSON.parse(localStorage.getItem("profile"))
      : {},
  },
  getters: {
    getProfile(state) {
      return state.profile;
    },
    shoppNum(state) {
      if (state.profile.shoppingcart) {
        //判断shoppcart里面有没有字段有的话则将其解析出来没有的话让其成为一个空数组
        return JSON.parse(state.profile.shoppingcart).length;
      }
      return 0;
    },
    address(state) {
      if (state.profile.myaddress) {
        return JSON.parse(state.profile.myaddress);
      } else {
        return [];
      }
    },
    //用户信息里面购物车的信息
    shoppData(state) {
      if (state.profile.shoppingcart) {
        return JSON.parse(state.profile.shoppingcart);
      } else {
        return 0;
      }
    },
  },
  mutations: {
    setProfile(state, payload) {
      //修改数据
      state.profile = payload.data;
      localStorage.setItem("profile", JSON.stringify(payload.data));
    },
    setProfileaddress(state, payload) {
      state.profile.myaddress = JSON.stringify(payload.data); //设置全局的storeaddress的值将其转化成json格式
      localStorage.setItem("profile", JSON.stringify(state.profile));
    },
  },
  actions: {
    //设置nick的actions方法
    setprofileActions(context, payload) {
      context.commit({
        ...payload,
        type: "setProfile",
      });
    },
    setprofileActionsaddress(context, payload) {
      return new Promise(function (reslove, reject) {
        updateProfile(
          {
            filedname: "myaddress",
            filedvalue: JSON.stringify(payload.data),
          },
          () => {
            context.commit({
              ...payload,
              type: "setProfileaddress",
            });
            return reslove();
          }
        );
      });
    },
  },
};
